package com.sukumar.android.backit.service;

import java.io.IOException;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.os.Vibrator;

public class BackItNotifierService extends Service {
	MediaPlayer mMediaPlayer;
	Vibrator mVibrator;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		final int repeat = intent.getExtras().getInt("repeat");
		Thread t = null;
		if (t == null && repeat != -99) {
			t = new Thread(BackItNotifierService.class.toString()) {
				public void run() {
					try {
						sleep(repeat*1000);
					} catch (InterruptedException e) {
					}
					stopService(new Intent(getApplicationContext(),
							BackItNotifierService.class));
				}

			};
			t.start();
		}

		playNotification(getApplicationContext(), intent);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mMediaPlayer.stop();
		mVibrator.cancel();
	}

	private void playNotification(Context pContext, Intent pintent) {
		mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		mMediaPlayer = new MediaPlayer();
		boolean prefVibration = pintent.getExtras().getBoolean("vibrate");
		String strUri = pintent.getExtras().getString("URI");
		String strPattern = pintent.getExtras().getString("pattern");

		try {
			if (strUri != null && !strUri.equals("")) {
				Uri ringtoneUri = Uri.parse(strUri);
				mMediaPlayer = new MediaPlayer();
				mMediaPlayer.setDataSource(pContext, ringtoneUri);
			} else {
				Uri bkpUri = null;
				if (strPattern.equals("BING")) {
					bkpUri = RingtoneManager
							.getDefaultUri(RingtoneManager.TYPE_ALARM);
					if (bkpUri == null) {
						bkpUri = RingtoneManager
								.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
					}

				} else {
					bkpUri = Uri.parse(strUri);
				}
				mMediaPlayer.setDataSource(pContext, bkpUri);
			}

			final AudioManager audioManager = (AudioManager) pContext
					.getSystemService(Context.AUDIO_SERVICE);
			int maxVolume = audioManager
					.getStreamMaxVolume(AudioManager.STREAM_ALARM);
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume,
					AudioManager.ADJUST_RAISE);

			if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
				mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
				mMediaPlayer.setLooping(true);
				mMediaPlayer.prepare();
				mMediaPlayer.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (prefVibration) {
			int dot = 200; // Length of a Morse Code "dot" in milliseconds
			int dash = 500; // Length of a Morse Code "dash" in milliseconds
			int short_gap = 200; // Length of Gap Between dots/dashes
			int medium_gap = 500; // Length of Gap Between Letters
			int long_gap = 1000; // Length of Gap Between Words
			long[] pattern = { 0, dot, short_gap, dot, short_gap, dot,
					medium_gap, dash, short_gap, dash, short_gap, dash,
					medium_gap, dot, short_gap, dot, short_gap, dot, long_gap };
			mVibrator.vibrate(pattern, 0);
		}
	}

}
